package com.ailot.cloud.mall.controller;

import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.base.log.annotation.Oplog;
import com.ailot.cloud.mall.entity.GoodsSpec;
import com.ailot.cloud.mall.service.GoodsSpecService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 规格表
 *
 * @Author ailot
 * @create 2024-06-30
 */

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/goodsspec")
public class GoodsSpecController {

    private final GoodsSpecService goodsSpecService;

    /**
     * 分页
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('mall:goodsspec:page')")
    public Page<GoodsSpec> getGoodsSpecPage(Page<GoodsSpec> page, GoodsSpec goodsSpec) {
        return goodsSpecService.page(page, Wrappers.query(goodsSpec));
    }

    /**
     * 新增
     */
    @Oplog("新增")
    @PostMapping
    @PreAuthorize("hasAuthority('mall:goodsspec:add')")
    public Boolean save(@Valid @RequestBody GoodsSpec goodsSpec) {
        return goodsSpecService.save(goodsSpec);
    }

    /**
     * 修改
     */
    @Oplog("修改")
    @PutMapping
    @PreAuthorize("hasAuthority('mall:goodsspec:edit')")
    public Boolean update(@Valid @RequestBody GoodsSpec goodsSpec) {
        return goodsSpecService.updateById(goodsSpec);
    }

    /**
     * 删除
     */
    @Oplog("删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('mall:goodsspec:del')")
    public Result delete(@PathVariable String id) {
        return Result.success(goodsSpecService.removeById(id));
    }


}
